<template>
  <div>
    <form>
      <div class="form-group">
        <label class="col-sm-12 control-label"> <img src="../../../static/optionright.png" width="25" height="25"></img>&nbsp;燃气表照片：</label>
        <div class="col-sm-12">
          <img  :class="paper.style__" @click="takePic('f_meter_path', '燃气表照片')"  :src="meter.f_meter_path" alt="燃气表照片" width="150" height="200">
          &nbsp;&nbsp;&nbsp;&nbsp;<span  :class="paper.style__" class="glyphicon glyphicon-trash" @click="delfile('f_meter_path', this.meter.f_meter_path)"></span>
          &nbsp;&nbsp;&nbsp;&nbsp;<span class="glyphicon glyphicon-picture" @click="this.$parent.$parent.viewPic(this.meter.f_meter_path)"></span>
        </div>
      </div>
      <div :class="paper.style__" class="form-group">
        <label class="col-sm-12 control-label">
          <img src="../../../static/optionright.png" width="25" height="25"></img>&nbsp;
          卡号：</label>
        <div class="col-sm-12">
          <input type="text" class="form-control" v-model="meter.f_card_id" maxlength="10">
        </div>
      </div>
      <div :class="paper.style__" class="form-group">
        <label class="col-sm-12 control-label">
          <img src="../../../static/optionright.png" width="25" height="25"></img>&nbsp;
          燃气表生产厂家</label>
        <div class="col-sm-12">
          <radio-group :value.sync="meter.f_maker" type="success" buttons="false">
            <radio value="成都秦川">成都秦川</radio>
            <radio value="陕西秦川">陕西秦川</radio>
            <radio value="山城">山城</radio>
            <radio value="航天">航天</radio>
            <radio value="其他">其他</radio>
          </radio-group>
        </div>
      </div>
      <div :class="paper.style__" class="form-group">
          <label class="col-sm-12 control-label">
            <img src="../../../static/optionright.png" width="25" height="25"></img>&nbsp;
            其他：</label>
          <div class="col-sm-12">
            <input type="text" class="form-control" v-model="meter.f_maker_" maxlength="20">
          </div>
      </div>

      <div :class="paper.style__" class="form-group">
        <label class="col-sm-12 control-label">
          <img src="../../../static/optionright.png" width="25" height="25"></img>&nbsp;
          表型</label>
        <div class="col-sm-12">
          <radio-group :value.sync="meter.f_diaphgram_size" type="success" buttons="false">
            <radio value="G1.6">G1.6</radio>
            <radio value="G2.5">G2.5</radio>
            <radio value="G4.0">G4.0</radio>
            <radio value="其他">其他</radio>
          </radio-group>
        </div>
        <label class="col-sm-12 control-label">
          <img src="../../../static/optionright.png" width="25" height="25"></img>&nbsp;
          其他：</label>
        <div class="col-sm-12">
          <input type="text" class="form-control" v-model="meter.f_diaphgram_size_" maxlength="20">
        </div>
      </div>
      <div :class="paper.style__" class="form-group">
        <label class="col-sm-12 control-label">
          <img src="../../../static/optionright.png" width="25" height="25"></img>&nbsp;
          基表号：</label>
        <div class="col-sm-12">
          <input type="text" class="form-control" maxlength="20" v-model="meter.f_meter_no" @keyup="resetReading">
        </div>
        <div class="col-sm-12">
          <a href="#" class="btn btn-primary" @click="updateMeterNo()">修改基表号</a>
        </div>
      </div>
      <div :class="paper.style__" class="form-group">
        <label class="col-sm-12 control-label">
          <img src="../../../static/optionright.png" width="25" height="25"></img>&nbsp;
          本期基表读数：</label>
        <div class="col-sm-12">
          <input type="text" class="form-control" maxlength="10" v-model="meter.f_reading" number @keyup="calcGasUsage">
        </div>
      </div>
      <div :class="paper.style__" class="form-group">
        <label class="col-sm-12 control-label">
          <img src="../../../static/optionright.png" width="25" height="25"></img>&nbsp;
          上期基表读数：</label>
        <div class="col-sm-12">
          <input type="text" readonly="readonly" class="form-control" maxlength="10" v-model="meter.f_prior_reading">
        </div>
        <div class="col-sm-12">
          <a href="#" class="btn btn-primary" @click="getLastReading()">提取读数</a>
        </div>
      </div>
      <div :class="paper.style__" class="form-group">
        <div class="col-sm-12">
            <input type="checkbox" v-model="meter.f_newmeter"  @click="calcGasUsage">换表</input>
        </div>
      </div>
      <div :class="paper.style__" class="form-group">
        <label class="col-sm-12 control-label">
          <img src="../../../static/optionright.png" width="25" height="25"></img>&nbsp;
          本期用气量：</label>
        <div class="col-sm-12">
          <input type="text" :disabled="!meter.f_newmeter" class="form-control" maxlength="10" v-model="meter.f_usage" number>
        </div>
      </div>
      <div :class="paper.style__" class="form-group">
        <label class="col-sm-12 control-label">
          <img src="../../../static/optionright.png" width="25" height="25"></img>&nbsp;
          表内剩余气量：</label>
        <div class="col-sm-12">
          <input type="text" class="form-control" maxlength="10" v-model="meter.f_remaining_gas" number>
        </div>
      </div>
      <div :class="paper.style__" class="form-group">
        <label class="col-sm-12 control-label">
          <img src="../../../static/optionright.png" width="25" height="25"></img>&nbsp;
          燃气表生产日期（年）：</label>
        <div class="col-sm-12">
        <datepicker id="f_making_date" placeholder="燃气表生产日期（年）"
          :value.sync="f_making_date"
          :disabled-days-of-Week="[]"
          :format="'yyyy'"
          :show-reset-button="reset">
        </datepicker>
        </div>
      </div>
      <div :class="paper.style__" class="form-group">
        <div class="col-sm-12">
            <input type="checkbox" v-model="meter.f_invalid">报废</input>
        </div>
      </div>
      <div class="form-group" v-show="meter.f_invalid">
        <label class="col-sm-12 control-label">报废照片：</label>
        <div class="col-sm-12">
          <img :class="paper.style__" @click="takePic('f_invalid_path', '报废照片')"  :src="meter.f_invalid_path" alt="报废照片" width="150" height="200">
          &nbsp;&nbsp;&nbsp;&nbsp;<span :class="paper.style__" class="glyphicon glyphicon-trash" @click="delfile('f_invalid_path', this.meter.f_invalid_path)"></span>
          &nbsp;&nbsp;&nbsp;&nbsp;<span class="glyphicon glyphicon-picture" @click="this.$parent.$parent.viewPic(this.meter.f_invalid_path)"></span>
        </div>
      </div>
      <div :class="paper.style__" class="form-group">
        <label class="col-sm-12 control-label">
          <img src="../../../static/optionright.png" width="25" height="25"></img>&nbsp;
          进气方向</label>
        <div class="col-sm-12">
          <radio-group :value.sync="meter.f_flow_direction" type="success" buttons="false">
            <radio value="左表">左表</radio>
            <radio value="右表">右表</radio>
          </radio-group>
        </div>
      </div>
      <div :class="paper.style__" class="form-group">
        <label class="col-sm-12 control-label">
          <img src="../../../static/optionright.png" width="25" height="25"></img>&nbsp;
          安检项</label>
        <div class="col-sm-12">
            <input type="checkbox" v-model="meter.f_defect_no">正常</input>
            <input type="checkbox" v-model="meter.f_defect_wrapped">包裹</input>
            <input type="checkbox" v-model="meter.f_defect_leakage">漏气</input>
        </div>
      </div>
      <div class="form-group" v-show="meter.f_defect_wrapped">
        <label class="col-sm-12 control-label">包裹照片：</label>
        <div class="col-sm-12">
          <img :class="paper.style__" @click="takePic('f_wrapped_path', '包裹照片')"  :src="meter.f_wrapped_path" alt="包裹照片" width="150" height="200">
          &nbsp;&nbsp;&nbsp;&nbsp;<span :class="paper.style__" class="glyphicon glyphicon-trash" @click="delfile('f_wrapped_path', this.meter.f_wrapped_path)"></span>
          &nbsp;&nbsp;&nbsp;&nbsp;<span class="glyphicon glyphicon-picture" @click="this.$parent.$parent.viewPic(this.meter.f_wrapped_path)"></span>
        </div>
      </div>
      <div :class="paper.style__" class="form-group">
        <label class="col-sm-12 control-label">
          <img src="../../../static/optionright.png" width="25" height="25"></img>&nbsp;
          表故障</label>
        <div class="col-sm-12">
          <radio-group :value.sync="meter.f_meter_defect" type="success" buttons="false">
            <radio value="正常">正常</radio>
            <radio value="长通表">长通表</radio>
            <radio value="死表">死表</radio>
            <radio value="表不过气">表不过气</radio>
          </radio-group>
        </div>
      </div>
      <div :class="paper.style__" class="form-group">
        <label class="col-sm-12 control-label">
          <img src="../../../static/optionright.png" width="25" height="25"></img>&nbsp;
          腐蚀</label>
        <div class="col-sm-12">
          <radio-group :value.sync="meter.f_meter_erosion" type="success" buttons="false">
            <radio value="正常">正常</radio>
            <radio value="严重">严重</radio>
            <radio value="中度">中度</radio>
            <radio value="轻微">轻微</radio>
          </radio-group>
        </div>
      </div>
      <div class="form-group" v-show="meter.f_meter_erosion=='严重'">
        <label class="col-sm-12 control-label">严重腐蚀照片：</label>
        <div class="col-sm-12">
          <img :class="paper.style__" @click="takePic('f_erosion_path', '严重腐蚀照片')"  :src="meter.f_erosion_path" alt="严重腐蚀照片" width="150" height="200">
          &nbsp;&nbsp;&nbsp;&nbsp;<span :class="paper.style__" class="glyphicon glyphicon-trash" @click="delfile('f_erosion_path', this.meter.f_erosion_path)"></span>
          &nbsp;&nbsp;&nbsp;&nbsp;<span class="glyphicon glyphicon-picture" @click="this.$parent.$parent.viewPic(this.meter.f_erosion_path)"></span>
        </div>
      </div>
      <div :class="paper.style__" class="form-group">
        <label class="col-sm-12 control-label">
          <img src="../../../static/optionright.png" width="25" height="25"></img>&nbsp;
          备注：</label>
        <div class="col-sm-12">
          <input type="text" class="form-control" v-model="meter.f_remark">
        </div>
      </div>
    </form>
    <a href="#" class="btn btn-success" @click="goBackValidation">确定</a>
    <div style="height:50px;"></div>
  </div>
</template>

<script>
import Vue from 'vue'
import * as Util from '../Util'
import RadioGroup from 'vue-strap/src/radioGroup'
import Radio from 'vue-strap/src/radioBtn'

export default {
  title: '燃气表',
  data() {
    return {
      paper: this.$parent.$parent.data,
      f_making_date: ''
    }
  },
  props: ['idx'],
  components: {
    RadioGroup,
    Radio
  },
  ready() {
    this.$set('meter', this.paper.f_meters[this.idx])
    this.meter.f_card_id = this.paper.f_card_id
    if(!this.meter.f_meter_path)
      this.$set('meter.f_meter_path', Vue.nopic)
    if(!this.meter.f_erosion_path)
      this.$set('meter.f_erosion_path', Vue.nopic)
    //清楚照片
    this.$watch('meter.f_meter_erosion', function (val) {
      if(val != '严重') {
        this.meter.f_erosion_path = Vue.nopic
      }
    })
    if(!this.meter.f_wrapped_path)
      this.$set('meter.f_wrapped_path', Vue.nopic)
    this.$watch("meter.f_defect_wrapped", function(val){
      if(!val)
        this.$set('meter.f_wrapped_path', Vue.nopic)
    })
    if(!this.meter.f_invalid_path)
      this.$set('meter.f_invalid_path', Vue.nopic)
    this.$watch("meter.f_invalid", function(val){
      if(!val)
        this.$set('meter.f_invalid_path', Vue.nopic)
    })

    this.$watch('meter.f_maker', function(val){
      if(val != '其他') {
        this.$set('meter.f_maker_', null)
      }
    })
    this.$watch('meter.f_diaphgram_size', function(val){
      if(val != '其他') {
        this.$set('meter.f_diaphgram_size_', null)
      }
    })

    if(!this.meter.f_making_date) {
      this.$set('meter.f_making_date', null)
    } else {
      this.f_making_date = this.meter.f_making_date + ''
    }

    this.$watch('f_making_date', function (val) {
        this.meter.f_making_date = val/1
    })

  },
  methods: {
    cameraCallBack(prop, fileName) {
      console.log(prop + ',' + fileName)
      //HostApp.__this__.$set(prop, fileName) doesnt work
      HostApp.__this__.meter[prop] = fileName + '?' + Math.random()
      console.log(HostApp.__this__.meter[prop])
      HostApp.__callback__ = null
      HostApp.__this__ = null
    },
    takePic(prop, title) {
      HostApp.__callback__ = this.cameraCallBack
      HostApp.__this__ = this
      console.log('this.meter[prop]:' + this.meter[prop])
      let fileName
      if(!this.meter[prop] || this.meter[prop].includes("nopic.png")) {
        fileName = Util.guid() +'-meter-' + this.idx + '-' + prop + '.jpg'
      } else {
        fileName = Util.getFileName(this.meter[prop])
      }
      HostApp._open_a_page({type: 'boomerang',
        page: 'com.aofeng.hybrid.android.peripheral.CameraActivity',
        param: {file: fileName, requestCode: 111, callback:'javascript:HostApp.__callback__("' + prop +'", "%s");', watermark: title + '\t时间：'+ Util.toStandardTimeString() + '\t' + Vue.user.name}
      })
    },
    goBackValidation() {
      this.$goto('paper-device', {}, 'paper-router')
    },

    delfile(prop, fileName) {
      if(fileName == Vue.nopic)
        return
      else {
        HostApp.delfile(fileName)
        this.meter[prop] = Vue.nopic
      }
    },

    // 提取上期基表读数
    getLastReading () {
      if(!this.meter.f_meter_no) {
        HostApp.alert('请先填写基表号！')
      } else {
        let that = this
        this.$post(Vue.url + 'logic/提取基表读数', {data:{f_meter_no: this.meter.f_meter_no, f_user_id: this.paper.f_user_id}}, {resolveMsg: null, rejectMsg: null}).then(
          (response) => {
            that.$set('meter.f_prior_reading', response.data.f_prior_reading)
            that.calcGasUsage()
          }
        ).catch(
          (response) => {
            HostApp.alert('提取上期基表读数失败！')
          }
        )
      }
    },
    updateMeterNo () {
      if (!this.mete.f_meter_no) {
        HostApp.alert('请先输入表号')
        return
      }
      HostApp.updateMeterNo(this.meter.f_meter_no, this.paper.f_check_item_id)
    },
    // 计算本期读数
    calcGasUsage () {
      if(Util.isInt(this.meter.f_prior_reading) && Util.isInt(this.meter.f_reading)) {
        let v = Math.round(this.meter.f_reading - (this.meter.f_prior_reading / 1))
        this.$set('meter.f_usage', v)
      }
    },

    //重置抄表
    resetReading () {
      this.$set('meter.f_prior_reading', '')
      this.$set('meter.f_usage', '')
      this.$set('meter.f_newmeter', false)
    },
  }
}
</script>
